package com.ch.pagedemo.pading

import android.os.SystemClock
import androidx.paging.PositionalDataSource
import com.ch.pagedemo.bean.Movie

class MovieDataSource : PositionalDataSource<Movie>() {
    var listData = ArrayList<Movie>()
    var tmepNumber = 10;
    override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Movie>) {
        for (i in 0 until 10) {
            val movie = Movie("title_$i", "cover_$i", "rata_$i")
            listData.add(movie)
        }
        callback.onResult(listData, 0, 100)
    }

    override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Movie>) {
        SystemClock.sleep(1500)
        var tepListData = ArrayList<Movie>()
        var toatle = tmepNumber + 10
        for (i in tmepNumber until toatle) {
            val movie = Movie("title_$i", "cover_$i", "rata_$i")
            tepListData.add(movie)
        }
        callback.onResult(tepListData)
        tmepNumber = toatle
    }
}